Rico's blog.

daemon执行ldrestart问题

字数统计: 374阅读时长: 1 min
2020/05/29 Share

daemon执行ldrestart问题

0x01

在daemon中调用ldrestart的时候发现一个问题,ldrestart不能完整执行,经过问题定位发现执行ldrestart后,自身也被ldrestart命令kill掉了,所以没有继续执行下去。

0x02

怎么才能执行ldrestart的时候跳过自身daemon,通过论坛提问及自己思考,通过修改ldrestart源码及重新编译后能完美解决,但是有了源码怎么才能变成一个可执行的且强大的ldrestart呢,最后决定模仿root daemon来解决。

0x03

比较后发现自定义ldrestart只是不需要launchctl 且 把可执行文件copy到/usr/bin目录下即可

so 修改postinst,执行 cp 命令

0x04

上源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <cstdio>
#include <cstdlib>

#include <errno.h>
#include <signal.h>
#include <sysexits.h>
#include <unistd.h>

#include <launch.h>

#include <sys/stat.h>

#include "LYRunloop.h"

void process(launch_data_t value, const char *name, void *baton) {
if (!strstr(name, "apple"))
return;

if (launch_data_get_type(value) != LAUNCH_DATA_DICTIONARY)
return;

auto integer(launch_data_dict_lookup(value, LAUNCH_JOBKEY_PID));
if (integer == NULL || launch_data_get_type(integer) != LAUNCH_DATA_INTEGER)
return;


auto pid(launch_data_get_integer(integer));
if (kill(pid, 0) == -1)
return;

auto string(launch_data_dict_lookup(value, LAUNCH_JOBKEY_LABEL));
if (string == NULL || launch_data_get_type(string) != LAUNCH_DATA_STRING)
return;
auto label(launch_data_get_string(string));

auto stop(launch_data_alloc(LAUNCH_DATA_DICTIONARY));
launch_data_dict_insert(stop, string, LAUNCH_KEY_STOPJOB);

auto result(launch_msg(stop));
launch_data_free(stop);
if (result == NULL)
return;

if (launch_data_get_type(result) != LAUNCH_DATA_ERRNO)
NSLog(@"yr:ioser:%s\n", label);
else if (auto number = launch_data_get_errno(result))
NSLog(@"yr:%s: %s\n", label, strerror(number));

launch_data_free(result);
}

int main(int argc, char **argv, char **envp) {
NSLog(@"ldrestar: ldrestar start");
auto request(launch_data_new_string(LAUNCH_KEY_GETJOBS));
auto response(launch_msg(request));
launch_data_free(request);

if (response == NULL)
return EX_UNAVAILABLE;
if (launch_data_get_type(response) != LAUNCH_DATA_DICTIONARY)
return EX_SOFTWARE;
launch_data_dict_iterate(response, &process, NULL);
return EX_OK;

}

以上源码在coolstart

coolstar/uikittools-theos

CATALOG
  1. 1. daemon执行ldrestart问题
    1. 1.1. 0x01
    2. 1.2. 0x02
    3. 1.3. 0x03
    4. 1.4. 0x04